package com.polaris.live.viewmodel.party

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.BizEventConst
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.event.EventMessage
import com.polaris.live.common.network.request
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.im.sfs.bean.live.ext.UserBasicVo
import com.polaris.live.net.liveService
import com.polaris.live.net.userService
import com.polaris.live.popup.GlobalPopupManager
import com.polaris.live.resp.back_resp.InfoOtherResp
import com.polaris.live.resp.back_resp.PartyRoomResp
import com.polaris.live.resp.back_resp.RejectSiteResp
import com.polaris.live.resp.back_resp.UserFreeGiftResp
import com.polaris.live.resp.go_resp.FollowRoomRequest
import com.polaris.live.resp.go_resp.LiveFollowBean
import com.polaris.live.resp.go_resp.SendGiftBean
import com.polaris.live.resp.go_resp.SendPartyGiftBean
import com.polaris.live.translation.R
import com.polaris.live.utils.BitwiseUtils
import org.greenrobot.eventbus.EventBus

/**
 * PartySpectatorFragmentModel  观众Model
 * @author Created by 天晴 on 2024/2/24 11:15
 * @since 1.0.0
 **/
open class PartySpectatorFragmentModel : PartyAnchorFragmentModel() {

    //获取用户信息
    val infoLiveData = MutableLiveData<ResultState<InfoOtherResp>>()
    fun getInfoUser(id: Long, vararg type: Long) {
        val bitType = BitwiseUtils.bitwiseOperation(type)
        request({ userService.getInfoOther(id, bitType) }, infoLiveData)
    }

    /**
     * 拒绝上麦
     */
    val partyRejectSiteData = MutableLiveData<ResultState<Any>>()
    fun partyRejectSite(liveId: Long) {
        request(
            { liveService.partyRejectSite(RejectSiteResp(liveId)) },
            partyRejectSiteData
        )
    }

    /**
     * 送礼物
     * type是礼物类型
     * 其他的值就代表关注礼物的下标  因为需要这个下标去删除对应的消息
     */
    val sendFollowerLiveData = MutableLiveData<Int?>()
    fun sendGift(
        context: Context,
        sendGiftBean: SendGiftBean,
        type: Int,
    ) {
        requestLoading({ liveService.sendGift(sendGiftBean) }, {

            sendFollowerLiveData.value = type
        }, {
            ToastGravity.showGravity(it.errorMsg)
            if (it.errCode == CommonConst.ErrorCode.SENDGIFT) {
                GlobalPopupManager.showRechargePopup(context, this, isNeedBottom = true, onRechargeOk = {
                    EventBus.getDefault().post(EventMessage(BizEventConst.INFO_UPDATA))
                })
            }
        })
    }

    //直播关注
    val liveFollowLiveData = MutableLiveData<ResultState<Any>>()
    fun addLiveFollow(otherId: Long, liveId: Long) {
        request({ liveService.liveFollow(LiveFollowBean(liveId, otherId)) }, liveFollowLiveData)
    }

    //房间关注
    val followRoomLiveData = MutableLiveData<ResultState<Any>>()
    fun addFollowRoom(otherId: Long) {
        request({ liveService.followRoom(FollowRoomRequest(otherId)) }, followRoomLiveData)
    }

    //获取房间关注
    val isFollowRoomLiveData = MutableLiveData<ResultState<Boolean>>()
    fun getFollowRoomUser(id: Long) {
        request({ liveService.isFollowRoom(id) }, isFollowRoomLiveData)
    }

    //礼物飘屏用户数据
    val giftInfoLiveData = MutableLiveData<ResultState<InfoOtherResp>>()
    fun getGiftInfoUser(id: Long) {
        val type = BitwiseUtils.bitwiseOperation(
            longArrayOf(
                UserManager.BASIC_INFORMATION,
                UserManager.LIVE_INFORMATION,
            )
        )
        request({ userService.getInfoOther(id, type) }, giftInfoLiveData)
    }

    val mPartyRoomLiveData = MutableLiveData<ResultState<PartyRoomResp>>()
    fun getPartyRoom() {
        request({ liveService.getPartyRoom() }, mPartyRoomLiveData)
    }

    //赠送免费礼物
    val mSendFreeGiftLiveData = MutableLiveData<Any>()
    fun sendFreeGift(userbasic: UserBasicVo, gift: UserFreeGiftResp, sourceType: Int, objectId: Long) {
        val giftBean = gift.giftVo
        if (giftBean != null) {
            val sendGiftBean = SendPartyGiftBean(
                listOf(userbasic.userId), giftBean.id, 1, sourceType, objectId, giftBean.type, gift.backpackId,
                gift.giftVo.payload
            )
            request({ liveService.batchSend(sendGiftBean) }, {
                ToastGravity.showGravity(R.string.gift_send_success)
                mSendFreeGiftLiveData.value = it
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }
    }
}